package com.splic.web.controller.product;

import com.github.pagehelper.PageInfo;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.page.TableDataInfo;
import com.splic.common.utils.DateUtils;
import com.splic.common.utils.StringUtils;
import com.splic.product.domain.Channel;
import com.splic.product.domain.GoodsType;
import com.splic.product.domain.Product;
import com.splic.product.dto.OrderScaleDto;
import com.splic.product.dto.OrderStatisDto;
import com.splic.product.service.ChannelService;
import com.splic.product.service.GoodsTypeService;
import com.splic.product.service.OrderStatisService;
import com.splic.product.service.ProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 用户设备Controller
 * 
 * @author singe
 * @date 2022-01-04
 */
@Controller
@RequestMapping("/product/orderStatis")
public class OrderStatisController extends BaseController
{
    private String prefix = "product/statistics/orderStatis";

    @Autowired
    private ProductService productServiceImpl;
    @Autowired
    private GoodsTypeService goodsTypeServiceImpl;
    @Autowired
    private OrderStatisService orderStatisServiceImpl;
    @Autowired
    private ChannelService channelServiceImpl;

    @RequiresPermissions("product:orderStatis:view")
    @GetMapping()
    public String orderStatis(ModelMap mmap)
    {
        List<Channel> channelList = channelServiceImpl.selectChannelList(null);
        mmap.put("channelList",channelList);
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        mmap.put("startCreateTime", DateUtils.addDays(new Date(),-7));
        mmap.put("endCreateTime",new Date());

//        List<GoodsType> goodsTypeList = goodsTypeServiceImpl.selectGoodsTypeList(null);
//        mmap.put("goodsTypeList",goodsTypeList);

        return prefix + "/list";
    }

    /**
     * 查询用户设备列表
     */
    @RequiresPermissions("product:orderStatis:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderStatisDto orderStatisDto)
    {
        if (orderStatisDto.getViewProductId() == 0
                && orderStatisDto.getViewPayType() == 0
                && orderStatisDto.getViewGoodsType() == 0
                && orderStatisDto.getViewChannelId() == 0 ){
            orderStatisDto.setViewDate(1);
        }
        startPageNoOrderBy();
        List<OrderStatisDto> list = orderStatisServiceImpl.selectStatisPageList(orderStatisDto);
        OrderStatisDto dto = new OrderStatisDto();
        dto.setReturnCount(0);
//        dto.setReturnAvgAmount(0);
        dto.setReturnCountAmount(BigDecimal.ZERO);
        for (OrderStatisDto origin : list) {
            dto.setReturnCount(dto.getReturnCount() + origin.getReturnCount());
//            dto.setReturnAvgAmount(dto.getReturnAvgAmount() + origin.getReturnAvgAmount());
            dto.setReturnCountAmount(dto.getReturnCountAmount().add(origin.getReturnCountAmount()));
        }
        dto.setItem("总计");
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setTotal(new PageInfo(list).getTotal());
        list.add(dto);
        rspData.setRows(list);
        return rspData;
    }

    @RequiresPermissions("product:orderStatis:view")
    @GetMapping("/scale")
    public String orderStatisScale(ModelMap mmap)
    {
        List<Channel> channelList = channelServiceImpl.selectChannelList(null);
        mmap.put("channelList",channelList);
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        mmap.put("startCreateTime", DateUtils.addDays(new Date(),-7));
        mmap.put("endCreateTime",new Date());

//        List<GoodsType> goodsTypeList = goodsTypeServiceImpl.selectGoodsTypeList(null);
//        mmap.put("goodsTypeList",goodsTypeList);

        return prefix + "/scaleList";
    }

    /**
     * 查询用户设备列表
     */
    @RequiresPermissions("product:orderStatis:list")
    @PostMapping("/scaleList")
    @ResponseBody
    public TableDataInfo scaleList(OrderScaleDto orderScaleDto)
    {
        List<OrderScaleDto> result = new ArrayList<>();
        startPageNoOrderBy();
        List<OrderScaleDto> list = orderStatisServiceImpl.selectStatisList(orderScaleDto);
        OrderScaleDto dtoCount = new OrderScaleDto();
        dtoCount.setReturnCount(0);
//        dtoCount.setReturnAvgAmount(0);
        dtoCount.setReturnCountAmount(BigDecimal.ZERO);
        LinkedHashMap<String, List<OrderScaleDto>> map = new LinkedHashMap<>();
        long count = 0;
        List<OrderScaleDto> tempList;
        if (orderScaleDto.getItemScale() == 0) {
            for (OrderScaleDto dto : list) {
                if (StringUtils.isEmpty(dto.getReturnItem())) {
                    if (map.containsKey("0")) {
                        map.get("0").add(dto);
                    } else {
                        tempList = new ArrayList<>();
                        tempList.add(dto);
                        map.put("0",tempList);
                    }
                } else {
                    if (map.containsKey(dto.getReturnItem())) {
                        map.get(dto.getReturnItem()).add(dto);
                    } else {
                        tempList = new ArrayList<>();
                        tempList.add(dto);
                        map.put(dto.getReturnItem(),tempList);
                    }
                }
                count += dto.getReturnCount();

                dtoCount.setReturnCount(dtoCount.getReturnCount() + dto.getReturnCount());
                dtoCount.setReturnCountAmount(dtoCount.getReturnCountAmount().add(dto.getReturnCountAmount()));
            }
        } else {
            for (OrderScaleDto dto : list) {
                if (map.containsKey(dto.getReturnItem())) {
                    map.get(dto.getReturnItem()).add(dto);
                } else {
                    tempList = new ArrayList<>();
                    tempList.add(dto);
                    map.put(dto.getReturnItem(),tempList);
                }
                count += dto.getReturnCount();
                dtoCount.setReturnCount(dtoCount.getReturnCount() + dto.getReturnCount());
                dtoCount.setReturnCountAmount(dtoCount.getReturnCountAmount().add(dto.getReturnCountAmount()));
            }
        }
        OrderScaleDto tempDto;
        long tempSum;
        for (String item : map.keySet()) {
            tempSum = map.get(item).stream().mapToLong(OrderScaleDto::getReturnCount).sum();
            tempDto = new OrderScaleDto();
            tempDto.setReturnItem(item);
            tempDto.setReturnCount(tempSum);
            tempDto.setReturnCountAmount(map.get(item).stream().map(OrderScaleDto::getReturnCountAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            tempDto.setReturnScale(BigDecimal.valueOf(tempSum * 100).divide(BigDecimal.valueOf(count), 4, RoundingMode.HALF_UP) + "%");
            result.add(tempDto);
        }
        dtoCount.setReturnItem("总计");
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setTotal(new PageInfo(result).getTotal());
        result.add(dtoCount);
        rspData.setRows(result);
        return rspData;
    }

    /**
     * 新增或修改保存其他服et务与卡群组绑定
     */
    @GetMapping("/addChangeProduct")
    @ResponseBody
    public List<GoodsType> addChangeProduct(Long productId)
    {
        GoodsType goodsType = new GoodsType();
        goodsType.setProductId(productId);
        return goodsTypeServiceImpl.selectGoodsTypeList(goodsType);
    }
}
